/*
 * Copyright (C) 2010 Moduad Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package yxt.push.org.androidpn.demoapp;

import yxt.push.org.androidpn.client.ServiceManager;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import cn.com.winning.ecare.activity.R;

/**
 * This is an androidpn client demo application.
 * 
 * @author Sehwan Noh (devnoh@gmail.com)
 */
public class DemoAppActivity extends Activity {

	private UpdateManager updateManager;
	public ServiceManager serviceManager;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.d("DemoAppActivity", "onCreate()...");

		super.onCreate(savedInstanceState);
		setContentView(R.layout.push_main);

		// Settings
//		Button okButton = (Button) findViewById(R.id.btn_settings);
//		okButton.setOnClickListener(new View.OnClickListener() {
//			public void onClick(View view) {
//				ServiceManager.viewNotificationSettings(DemoAppActivity.this);
//			}
//		});

		// Start the service
		// 创建新的服务
		// 实例化serviceManager，主要是从androidpn.properties获取到一些配置信息，然后放到SharedPreferences里
		serviceManager = new ServiceManager(this);
		// 设置推送的图标，还是把图标的ID放到SharedPreferences里
		serviceManager.setNotificationIcon(R.drawable.notification);
		// startService,跳转到yxt.push.org.androidpn.client.NotificationService里执行
		serviceManager.startService();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, Menu.FIRST, 0, "检查更新");
		menu.add(0, Menu.FIRST+1, 0, "设置");
		menu.add(0, Menu.FIRST+2, 0, "退出");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    super.onOptionsItemSelected(item);
		switch (item.getItemId()) {
		case 1:
			updateManager = new UpdateManager(DemoAppActivity.this);
			// 检查软件更新
			updateManager.checkUpdate();
			break;
		case 2:
			ServiceManager.viewNotificationSettings(DemoAppActivity.this);
			break;
		case 3:
			serviceManager.stopService();
			android.os.Process.killProcess(android.os.Process.myPid());
			break;
		}
		return true;

	}

}